#####################################
##REPLICATION FOR MCGHEE, ET AL.,####
##"A PRIMARY CAUSE OF PARTISANSHIP"##
#####################################

#Table A8 (Appendix)#

#Load Data#
library(Zelig)
library(sandwich)
library(MatchIt)
library(WhatIf)
setwd("XXXX") #ENTER DIRECTORY PATHNAME IN PLACE OF XXXX

legyrr <- read.csv("legislator primaries.rep.csv", header=TRUE)

#generate election year counter#
legyrr$yrcnt <- (legyrr$elec-1992)/2

#drop ideowt var, which is not used for this table#
legyrr$ideowt <- NULL

#drop missing data#
legyrr <- na.omit(legyrr)

#define years & states as factors for FE regression#
legyrr$icpsrst <- as.factor(legyrr$icpsrst)
legyrr$yrcnt <- as.factor(legyrr$yrcnt)

##Effect of SCOTUS blanket primary ruling: Republicans##
#AK#
legyrr.ak <- legyrr[legyrr$icpsrst!=71 & legyrr$icpsrst!=73 & legyrr$elec>1994,]
legyrr.ak <- legyrr[legyrr$icpsrst==81 | legyrr$semiop==1,]
legyrr.ak$postsc <- as.integer(legyrr.ak$icpsrst==81 & legyrr.ak$elec>2000)
legyrr.ak$after00 <- as.integer(legyrr.ak$elec>2000)
legyrr.ak$ak <- as.integer(legyrr.ak$icpsrst==81)
m.out <- matchit(ak ~ pvote + yrcnt,
	data=legyrr.ak, method="nearest")
summary(m.out)
m.data <- match.data(m.out)
z.out <- zelig(pred_np ~ postsc + pvote + icpsrst + yrcnt, 
	robust=list(method="vcovHAC"), model="ls", data=m.data)
summary(z.out)
length(m.data$icpsrst)


#alternative modeling approach#
z.out <- zelig(pred_np ~ postsc + ak + after00, 
	model="ls", data=m.data)
summary(z.out)
length(m.data$icpsrst)

#CA#
legyrr.ca <- legyrr[legyrr$icpsrst!=81 & legyrr$icpsrst!=73 & legyrr$elec>1996,]
legyrr.ca <- legyrr[legyrr$icpsrst==71 | legyrr$semicl==1,]
legyrr.ca$postsc <- as.integer(legyrr.ca$icpsrst==71 & legyrr.ca$elec>2000)
legyrr.ca$after00 <- as.integer(legyrr.ca$elec>2000)
legyrr.ca$ca <- as.integer(legyrr.ca$icpsrst==71)
m.out <- matchit(ca ~ pvote + yrcnt,
	data=legyrr.ca, method="nearest")
summary(m.out)
m.data <- match.data(m.out)
z.out <- zelig(pred_np ~ postsc + pvote + icpsrst + yrcnt, 
	robust=list(method="vcovHAC"), model="ls", data=m.data)
summary(z.out)
length(m.data$icpsrst)

#alternative modeling approach#
z.out <- zelig(pred_np ~ postsc + ca + after00, 
	model="ls", data=m.data)
summary(z.out)
length(m.data$icpsrst)

#WA#
legyrr.wa <- legyrr[legyrr$icpsrst!=81 & legyrr$icpsrst!=71 & legyrr$elec<2008,]
legyrr.wa <- legyrr[legyrr$icpsrst==73 | legyrr$open==1,]
legyrr.wa$postsc <- as.integer(legyrr.wa$icpsrst==73 & legyrr.wa$elec>2002)
legyrr.wa$after02 <- as.integer(legyrr.wa$elec>2002)
legyrr.wa$wa <- as.integer(legyrr.wa$icpsrst==73)
m.out <- matchit(wa ~ pvote + yrcnt,
	data=legyrr.wa, method="nearest")
summary(m.out)
m.data <- match.data(m.out)
z.out <- zelig(pred_np ~ postsc + pvote + icpsrst + yrcnt, 
	robust=list(method="vcovHAC"), model="ls", data=m.data)
summary(z.out)
length(m.data$icpsrst)

#alternative modeling approach#
z.out <- zelig(pred_np ~ postsc + wa + after02, 
	model="ls", data=m.data)
summary(z.out)
length(m.data$icpsrst)

